import 'dart:math';

import 'package:epidemic_data/config/palette.dart';
import 'package:epidemic_data/widgets/country_dropdown.dart';
import 'package:epidemic_data/widgets/country_stats_grid.dart';
import 'package:epidemic_data/widgets/custom_app_bar.dart';
import 'package:flutter/material.dart';

class CountryStatsScreen extends StatefulWidget {
  @override
  _CountryStatsScreenState createState() {
    return _CountryStatsScreenState();
  }
}

class _CountryStatsScreenState extends State<CountryStatsScreen> {
  String? country = 'USA';
  int index = 0;
  List<String> countryList = ['USA', 'FRA', 'BR', 'NLD', 'DEU'];

  int getIndex(val) {
    return countryList.indexOf(val);
  }

  changeCountry(country) {
    setState(() {
      this.country = country;
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Palette.primaryColor,
      appBar: CustomAppBar(),
      body: CustomScrollView(
        physics: ClampingScrollPhysics(),
        slivers: <Widget>[
          SliverPadding(
            padding: const EdgeInsets.symmetric(horizontal: 10.0),
            sliver: SliverToBoxAdapter(
              child: Container(
                padding: const EdgeInsets.all(20.0),
                decoration: BoxDecoration(
                    color: Palette.primaryColor,
                    borderRadius: BorderRadius.only(
                      bottomLeft: Radius.circular(40.0),
                      bottomRight: Radius.circular(40.0),
                    )),
                child: Column(
                  crossAxisAlignment: CrossAxisAlignment.start,
                  children: <Widget>[
                    Row(
                      mainAxisAlignment: MainAxisAlignment.spaceBetween,
                      children: <Widget>[
                        Text(
                          '海外主要国家疫情数据',
                          style: const TextStyle(
                            color: Colors.white,
                            fontSize: 25.0,
                            fontWeight: FontWeight.bold,
                          ),
                        ),
                        CountryDropDown(
                          countries: countryList,
                          country: country,
                          onChanged: (val) => setState(
                              () => {country = val, index = getIndex(val),childKey.currentState!.getData(),}),
                        ),
                      ],
                    ),
                  ],
                ),
              ),
            ),
          ),
          SliverPadding(
            padding: const EdgeInsets.symmetric(horizontal: 10.0),
            sliver: SliverToBoxAdapter(
              child: CountryStatsGrid(index, key: childKey),
            ),
          ),
          // SliverPadding(
          //   padding: const EdgeInsets.only(top: 10.0),
          //   sliver: SliverToBoxAdapter(
          //     child: CovidBarChart(covidCases: covidUSADailyNewCases),
          //   ),
          // ),
        ],
      ),
    );
  }

}
